﻿/**
 * This is a part of MCServe, which is available under the Apache license 2.0.
 * With this code, you may do whatever you want as long as that is not in conflict with this license.
 * In addition, you should have a reference to the original project to include (http://mcserve.codeplex.com).
 * 
 * Author       : Lars Krämer ( Xe77a )
 * Website      : http://www.lkraemer.de
 * Project      : MCServe (http://mcserve.codeplex.com)
 * Copyright    : Copyright (c) 2013 lkraemer.de
 * License      : http://mcserve.codeplex.com/license
 * Description  : This is a part of the core functionality of the gui
 */

using System;
using System.Runtime.InteropServices;
using System.Windows.Media;
using System.Windows;
using System.Windows.Interop;

namespace MCServe
{
    public class DWMHelper
    {
        public static bool ExtendGlassFrame(Window window, Thickness margin)
        {
            if (!DwmIsCompositionEnabled())
            {
                BrushConverter Converter = new System.Windows.Media.BrushConverter();
                window.Background = (Brush)Converter.ConvertFromString("#FFB9D1EA");
                return false;
            }

            IntPtr hwnd = new WindowInteropHelper(window).Handle;
            if (hwnd == IntPtr.Zero)
                throw new InvalidOperationException("The Window must be shown before extending glass.");

            window.Background = Brushes.Transparent;
            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

            MARGINS margins = new MARGINS(margin);
            DwmExtendFrameIntoClientArea(hwnd, ref margins);
            return true;
        }

        public static bool EnableGlassEffect(Window window)
        {
            window.MouseLeftButtonDown += (s, e) =>
            {
                window.DragMove();
            };
            return EnableGlassEffect(window, true);
        }

        public static bool EnableGlassEffect(Window window, bool enabled)
        {
            return EnableGlassEffect(window, enabled, new Thickness(-1));
        }

        public static bool EnableGlassEffect(Window window, bool enabled, Thickness margin)
        {
            if (Environment.OSVersion.Version.Major < 6)
            {
                BrushConverter Converter = new System.Windows.Media.BrushConverter();
                window.Background = (Brush)Converter.ConvertFromString("#FFB9D1EA");
                return false;
            }

            if (!DwmIsCompositionEnabled())
            {
                BrushConverter Converter = new System.Windows.Media.BrushConverter();
                window.Background = (Brush)Converter.ConvertFromString("#FFB9D1EA");
                return false;
            }

            if (enabled)
            {
                IntPtr hwnd = new WindowInteropHelper(window).Handle;
                window.Background = Brushes.Transparent;
                HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
                MARGINS margins = new MARGINS(margin);
                DwmExtendFrameIntoClientArea(hwnd, ref margins);
            }
            else
            {
                //window.Background = SystemColors.WindowBrush;
                BrushConverter Converter = new System.Windows.Media.BrushConverter();
                window.Background = (Brush)Converter.ConvertFromString("#FFB9D1EA");
            }

            return true;
        }


        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();
    }


    struct MARGINS
    {
        public MARGINS(Thickness t)
        {
            Left = (int)t.Left;
            Right = (int)t.Right;
            Top = (int)t.Top;
            Bottom = (int)t.Bottom;
        }
        public int Left;
        public int Right;
        public int Top;
        public int Bottom;
    }

}
